package unit5.part9;

import java.util.Arrays;
import java.util.Comparator;

public class ReLambdaTest {
    public static void main(String[] args) {
        Student[] students = new Student[]{
                new Student("A", 100),
                new Student("B", 70),
                new Student("C", 80),
                new Student("D", 89),
        };

        // 要对比数据判断大小
        // Comparator: int compare(T o1, T o2);
        Comparator<Student> comparator = (f, s) -> f.score - s.score;

        // 如果要对比的数据是int类型则直接可以对比
        // Comparator comparingInt(Function intKey)
        comparator = Comparator.comparingInt(f -> f.score);
        comparator = Comparator.comparingInt(Student::getScore);

        // 对数组进行排序
        // Arrays: void sort(T[] a, Comparator c);
        Arrays.sort(students, comparator);

        for (Student student : students) {
            System.out.println(student.name);
        }
    }
}

class Student {
    String name;
    int score;

    public Student(String name, int score) {
        this.name = name;
        this.score = score;
    }

    public int getScore() {
        return score;
    }
}
